<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/base.html">
<link rel="import" href="/tracing/extras/chrome/cc/raster_task.html">
<link rel="import" href="/tracing/model/event_set.html">
<link rel="import" href="/tracing/ui/analysis/single_event_sub_view.html">
<link rel="import" href="/tracing/ui/extras/chrome/cc/raster_task_view.html">
<link rel="import" href="/tracing/ui/extras/chrome/cc/selection.html">

<script>
'use strict';

tr.exportTo('tr.ui.e.chrome.cc', function() {
  /**
   * @constructor
   */
  function RasterTaskSelection(selection) {
    tr.ui.e.chrome.cc.Selection.call(this);
    const whySupported = RasterTaskSelection.whySuported(selection);
    if (!whySupported.ok) {
      throw new Error('Fail: ' + whySupported.why);
    }
    this.slices_ = Array.from(selection);
    this.tiles_ = this.slices_.map(function(slice) {
      const tile = tr.e.cc.getTileFromRasterTaskSlice(slice);
      if (tile === undefined) {
        throw new Error('This should never happen due to .supports check.');
      }
      return tile;
    });
  }

  RasterTaskSelection.whySuported = function(selection) {
    if (!(selection instanceof tr.model.EventSet)) {
      return {ok: false, why: 'Must be selection'};
    }

    if (selection.length === 0) {
      return {ok: false, why: 'Selection must be non empty'};
    }

    let referenceSnapshot = undefined;
    for (const event of selection) {
      if (!(event instanceof tr.model.Slice)) {
        return {ok: false, why: 'Not a slice'};
      }

      const tile = tr.e.cc.getTileFromRasterTaskSlice(event);
      if (tile === undefined) {
        return {ok: false, why: 'No tile found'};
      }

      if (!referenceSnapshot) {
        referenceSnapshot = tile.containingSnapshot;
      } else {
        if (tile.containingSnapshot !== referenceSnapshot) {
          return {
            ok: false,
            why: 'Raster tasks are from different compositor instances'
          };
        }
      }
    }
    return {ok: true};
  };

  RasterTaskSelection.supports = function(selection) {
    return RasterTaskSelection.whySuported(selection).ok;
  };

  RasterTaskSelection.prototype = {
    __proto__: tr.ui.e.chrome.cc.Selection.prototype,

    get specicifity() {
      return 3;
    },

    get associatedLayerId() {
      const tile0 = this.tiles_[0];
      const allSameLayer = this.tiles_.every(function(tile) {
        tile.layerId === tile0.layerId;
      });
      if (allSameLayer) {
        return tile0.layerId;
      }
      return undefined;
    },

    get extraHighlightsByLayerId() {
      const highlights = {};
      this.tiles_.forEach(function(tile, i) {
        if (highlights[tile.layerId] === undefined) {
          highlights[tile.layerId] = [];
        }
        const slice = this.slices_[i];
        highlights[tile.layerId].push({
          colorKey: slice.title,
          rect: tile.layerRect
        });
      }, this);
      return highlights;
    },

    createAnalysis() {
      const sel = new tr.model.EventSet();
      this.slices_.forEach(function(slice) {
        sel.push(slice);
      });

      let analysis;
      if (sel.length === 1) {
        analysis = document.createElement('tr-ui-a-single-event-sub-view');
      } else {
        analysis = document.createElement('tr-ui-e-chrome-cc-raster-task-view');
      }
      analysis.selection = sel;
      return analysis;
    },

    findEquivalent(lthi) {
      // Raster tasks are only valid in one LTHI.
      return undefined;
    },

    // RasterTaskSelection specific stuff follows.
    get containingSnapshot() {
      return this.tiles_[0].containingSnapshot;
    }
  };

  return {
    RasterTaskSelection,
  };
});
</script>
